'usie strict'

module.exports = { byGrade: byGrade}
// byGrade(1);
// byGrade(2);
// byGrade(3);
// byGrade('自定义', 30, 2, 10, 13);

function byGrade(grade, num, step, range, calcu_type) {
  var result = [];
  if (arguments.length === 1 && grade === '一年级') {
    result.push(add_in_x(5));
    result.push(sub_in_x(5));
    result.push(add_in_x(5));
    result.push(sub_in_x(5));
    result.push(add_in_x(9));
    result.push(sub_in_x(9));
    result.push(add_in_x(9));
    result.push(sub_in_x(9));
    result.push(inverse_add_in_x(9));
    result.push(inverse_add_in_x(20));
    result.push(not_carry_add_in_x(20));
    result.push(carry_add_in_x(20));
    result.push(not_abd_sub_in_x(20));
    result.push(abd_sub_in_x(20));
    result.push(add_in_x(20));
    result.push(sub_in_x(20));
    result.push(not_carry_add_in_x(50));
    result.push(carry_add_in_x(50));
    result.push(not_abd_sub_in_x(50));
    result.push(abd_sub_in_x(50));
    result.push(inverse_add_in_x(50));
    result.push(add_in_x_3(10));
    result.push(sub_in_x_3(10));
    result.push(add_in_x_3(20));
    result.push(sub_in_x_3(20));
    result.push(add_in_x_3(50));
    result.push(sub_in_x_3(50));
    result.push(add_sub_in_x(9));
    result.push(add_sub_in_x(20));
    result.push(add_sub_in_x(50));
    // console.log(result);
    return result;
  } else if (arguments.length === 1 && grade === '二年级') {
    result.push(not_carry_add_in_x(99));
    result.push(not_abd_sub_in_x(99));
    result.push(not_carry_add_in_x(99));
    result.push(not_abd_sub_in_x(99));
    result.push(carry_add_in_x(99));
    result.push(abd_sub_in_x(99));
    result.push(carry_add_in_x(99));
    result.push(abd_sub_in_x(99));
    result.push(add_in_x(99));
    result.push(sub_in_x(99));
    result.push(mul_in_xy(0, 6));
    result.push(mul_in_xy(4, 9));
    result.push(mul_in_xy(0, 9));
    result.push(div_in_xy(1, 6));
    result.push(div_in_xy(4, 9));
    result.push(not_carry_add_by_x000());
    result.push(sub_by_x000());
    result.push(carry_add_by_x0_x00_x000(10));
    result.push(abd_sub_by_x0_x00_x000(10));
    result.push(carry_add_by_x0_x00_x000(100));
    result.push(abd_sub_by_x0_x00_x000(100));
    result.push(not_carry_add_xx0_and_xx0());
    result.push(sub_xx0_and_xx0());
    result.push(mul_add_in_x(9));
    result.push(mul_sub_in_x(9));
    result.push(div_add_in_x(9));
    result.push(div_sub_in_x(9));
    result.push(mul_in_10_3());
    result.push(div_in_10_3());
    result.push(div_mul_in_x_3(9));
    // console.log(result);
    return result;
  } else if (arguments.length === 1 && grade === '三年级') {
    result.push(add_in_x(100));
    result.push(sub_in_x(100));
    result.push(add_in_x(100));
    result.push(sub_in_x(100));
    result.push(add_in_x(1000));
    result.push(sub_in_x(1000));
    result.push(add_in_x(1000));
    result.push(sub_in_x(1000));
    result.push(div_in_xy(1, 9));
    result.push(div_in_xy(1, 9));
    result.push(div_in_xy(4, 9));
    result.push(mul_by_x_and_x0());
    result.push(mul_by_x_and_x0());
    result.push(mul_by_x_and_x0());
    result.push(div_have_remainder_in_x(9));
    result.push(div_have_remainder_in_x(9));
    result.push(mul_by_x0_and_x0());
    result.push(mul_by_x_and_x00());
    result.push(mul_by_x00_and_x0());
    result.push(mul_add_in_x(9));
    result.push(mul_sub_in_x(9));
    result.push(div_add_in_x(9));
    result.push(div_sub_in_x(9));
    result.push(add_in_x_3(100));
    result.push(sub_in_x_3(100));
    result.push(add_sub_in_x(100));
    result.push(add_mul_include_brackets(10));
    result.push(add_div_include_brackets(10));
    result.push(sub_mul_include_brackets(10));
    result.push(sub_div_include_brackets(10));
    // console.log(result);
    return result;
  } else if (arguments.length != 1 && grade === '自定义') {
    switch (calcu_type) {
      case 0: {  //加法
        if (step === 2) {
          for (var i = 0; i < num; i++) {
            result.push(add_in_x(range));
          }
        } else if (step === 3) {
          for (var i = 0; i < num; i++) {
            result.push(add_in_x_3(range));
          }
        } else {
          console.log("运算步数错误！");
        }
        break;
      } case 1: {  //进位加法
        if (step === 2) {
          for (var i = 0; i < num; i++) {
            result.push(carry_add_in_x(range));
          }
        } else if (step === 3) {
          for (var i = 0; i < num; i++) {
            result.push(add_in_x_3(range));
          }
        } else {
          console.log("运算步数错误！");
        }
        break;
      } case 2: {  //不进位加法
        if (step === 2) {
          for (var i = 0; i < num; i++) {
            result.push(not_carry_add_in_x(range));
          }
        } else if (step === 3) {
          for (var i = 0; i < num; i++) {
            result.push(add_in_x_3(range));
          }
        } else {
          console.log("运算步数错误！");
        }
        break;
      } case 3: {  //减法
        if (step === 2) {
          for (var i = 0; i < num; i++) {
            result.push(sub_in_x(range));
          }
        } else if (step === 3) {
          for (var i = 0; i < num; i++) {
            result.push(sub_in_x_3(range));
          }
        } else {
          console.log("运算步数错误！");
        }
        break;
      } case 4: {  //退位减法
        if (step === 2) {
          for (var i = 0; i < num; i++) {
            result.push(abd_sub_in_x(range));
          }
        } else if (step === 3) {
          for (var i = 0; i < num; i++) {
            result.push(sub_in_x_3(range));
          }
        } else {
          console.log("运算步数错误！");
        }
        break;
      } case 5: {  //不退位减法
        if (step === 2) {
          for (var i = 0; i < num; i++) {
            result.push(not_abd_sub_in_x(range));
          }
        } else if (step === 3) {
          for (var i = 0; i < num; i++) {
            result.push(sub_in_x_3(range));
          }
        } else {
          console.log("运算步数错误！");
        }
        break;
      } case 6: {  //加减混合
        for (var i = 0; i < num; i++) {
          result.push(add_sub_in_x(range));
        }
        break;
      } case 7: {  //乘法
        if (step === 2) {
          for (var i = 0; i < num; i++) {
            result.push(mul_in_xy(0, 9));
          }
        } else if (step === 3) {
          for (var i = 0; i < num; i++) {
            result.push(mul_in_10_3());  //三位乘法应在10以内
          }
        } else {
          console.log("运算步数错误！");
        }
        break;
      } case 8: {  //除法
        if (step === 2) {
          for (var i = 0; i < num; i++) {
            result.push(div_in_xy(1, 9));
          }
        } else if (step === 3) {
          for (var i = 0; i < num; i++) {
            result.push(div_in_10_3());  //三位除法应在10以内
          }
        } else {
          console.log("运算步数错误！");
        }
        break;
      } case 9: {  //乘加混合
        for (var i = 0; i < num; i++) {
          result.push(mul_add_in_x(9));
        }
        break;
      } case 10: {  //乘减混合
        for (var i = 0; i < num; i++) {
          result.push(mul_sub_in_x(9));
        }
        break;
      } case 11: {  //除加混合
        for (var i = 0; i < num; i++) {
          result.push(div_add_in_x(9));
        }
        break;
      } case 12: {  //除减混合
        for (var i = 0; i < num; i++) {
          result.push(div_sub_in_x(9));
        }
        break;
      } case 13: {  //除乘混合
        for (var i = 0; i < num; i++) {
          result.push(div_mul_in_x_3(9));
        }
        break;
      } default: ;
    }
    // console.log(result);
    return result;
  }
}

//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10);
    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
    default:
      return 0;
  }
}

function add_in_x(x) {  //数字x以内的两位加法
  var formula, ans, add1, add2;
  ans = randomNum(x);
  add1 = randomNum(x);
  if (ans < add1) {
    [ans, add1] = [add1, ans];
  }
  add2 = ans - add1;
  formula = [String(add1) + ' + ' + String(add2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function sub_in_x(x) { //数字x以内的两位减法
  var formula, ans, minuend, reduction;
  minuend = randomNum(x);
  reduction = randomNum(x);
  if (minuend < reduction) {
    [minuend, reduction] = [reduction, minuend];
  }
  ans = minuend - reduction;
  formula = [String(minuend) + ' - ' + String(reduction) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function inverse_add_in_x(x) {  //数字x以内的两位填括号加法 
  var formula, ans, add1, add2;
  ans = randomNum(x);
  add1 = randomNum(x);
  if (ans < add1) {
    [ans, add1] = [add1, ans];
  }
  add2 = ans - add1;
  formula = [String(add1) + ' + [ ] = ' + String(ans), add2];
  // console.log(formula);
  return formula;
}

function carry_add_in_x(x) {  //数字x以内的两位进位加法，应令x大于10
  var formula, ans, add1, add2;
  do {
    ans = randomNum(x);
    add1 = randomNum(x);
    if (ans < add1) {
      [ans, add1] = [add1, ans];
    }
    add2 = ans - add1;
  } while ((add2 % 10) + (add1 % 10) < 10);
  formula = [String(add1) + ' + ' + String(add2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function not_carry_add_in_x(x) {  //数字x以内的两位不进位加法
  var formula, ans, add1, add2;
  do {
    ans = randomNum(x);
    add1 = randomNum(x);
    if (ans < add1) {
      [ans, add1] = [add1, ans];
    }
    add2 = ans - add1;
  } while ((add2 % 10) + (add1 % 10) >= 10);
  formula = [String(add1) + ' + ' + String(add2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function abd_sub_in_x(x) {  //数字x以内的两位退位减法，应令x大于10
  var formula, ans, minuend, reduction;
  do {
    minuend = randomNum(x);
    reduction = randomNum(x);
    if (minuend < reduction) {
      [minuend, reduction] = [reduction, minuend];
    }
    ans = minuend - reduction;
  } while ((minuend % 10) >= (reduction % 10));
  formula = [String(minuend) + ' - ' + String(reduction) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function not_abd_sub_in_x(x) {  //数字x以内的两位不退位减法
  var formula, ans, minuend, reduction;
  do {
    minuend = randomNum(x);
    reduction = randomNum(x);
    if (minuend < reduction) {
      [minuend, reduction] = [reduction, minuend];
    }
    ans = minuend - reduction;
  } while ((minuend % 10) < (reduction % 10));
  formula = [String(minuend) + ' - ' + String(reduction) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function add_in_x_3(x) {  //数字x以内的三位连加
  var formula, ans, add1, add2, add3;
  ans = randomNum(3, x);
  add1 = randomNum(ans - 1);
  add2 = randomNum(ans - add1 - 1);
  add3 = ans - add1 - add2;
  formula = [String(add1) + ' + ' + String(add2) + ' + ' + String(add3) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function sub_in_x_3(x) {  //数字x以内的三位连减
  var formula, ans, add1, add2, add3;
  ans = randomNum(3, x);
  add1 = randomNum(ans - 1);
  add2 = randomNum(ans - add1 - 1);
  add3 = ans - add1 - add2;
  formula = [String(ans) + ' - ' + String(add2) + ' - ' + String(add3) + ' = [ ] ', add1];
  // console.log(formula);
  return formula;
}

function add_sub_in_x(x) {  //数字x以内的三位加减混合
  var formula, ans, add1, add2, add3;
  var i = randomNum(0, 1);
  ////console.log(i);
  if (i === 0) {  //加号在前
    add1 = randomNum(x);
    ans = randomNum(x);
    if (ans < add1) {
      [ans, add1] = [add1, ans];
    }
    add2 = ans - add1;
    add3 = randomNum(add1 + add2);
    ans = add1 + add2 - add3;
    formula = [String(add1) + ' + ' + String(add2) + ' - ' + String(add3) + ' = [ ] ', ans];
    // console.log(formula);
    return formula;
  } else {  //减号在前
    add1 = randomNum(x);
    add2 = randomNum(add1);
    add3 = randomNum(x - (add1 - add2));
    ans = add1 - add2 + add3;
    formula = [String(add1) + ' - ' + String(add2) + ' + ' + String(add3) + ' = [ ] ', ans];
    // console.log(formula);
    return formula;
  }
}

function mul_in_xy(x, y) {  //乘数在(x, y)内的乘法
  var formula, mul1, mul2, ans;
  mul1 = randomNum(x, y);
  mul2 = randomNum(x, y);
  ans = mul1 * mul2;
  formula = [String(mul1) + ' × ' + String(mul2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function div_in_xy(x, y) {  //除数和积在(x, y)内的整除
  var formula, mul1, mul2, ans;
  mul1 = randomNum(x, y);
  mul2 = randomNum(x, y);
  ans = mul1 * mul2;
  formula = [String(ans) + ' ÷ ' + String(mul2) + ' = [ ] ', mul1];
  // console.log(formula);
  return formula;
}

function not_carry_add_by_x000() {  //和在一万以内的整千加法
  var formula, add1, add2, ans;
  add1 = randomNum(9);
  add2 = 1000 * randomNum((8 - add1));
  add1 = 1000 * add1;
  ans = add1 + add2;
  formula = [String(add1) + ' + ' + String(add2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function sub_by_x000() {  //整千减法
  var formula, add1, add2, ans;
  add1 = randomNum(9);
  ans = 1000 * randomNum(add1, 9);
  add1 = 1000 * add1;
  add2 = ans - add1;
  formula = [String(ans) + ' - ' + String(add1) + ' = [ ] ', add2];
  // console.log(formula);
  return formula;
}

function carry_add_by_x0_x00_x000(x) {  //整x(十、百、千)的进位加法
  var formula, add1, add2, ans;
  add1 = randomNum(9);
  add2 = x * randomNum((10 - add1), 9);
  add1 = x * add1;
  ans = add1 + add2;
  formula = [String(add1) + ' + ' + String(add2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function abd_sub_by_x0_x00_x000(x) {  //整x(十、百、千)的退位减法
  var formula, add1, add2, ans;
  add1 = randomNum(9);
  add2 = x * randomNum((10 - add1), 9);
  add1 = x * add1;
  ans = add1 + add2;
  formula = [String(ans) + ' - ' + String(add2) + ' = [ ] ', add1];
  // console.log(formula);
  return formula;
}

function not_carry_add_xx0_and_xx0() {  //尾数是0三位数加法（和在一千以内）
  var formula, add1, add2, ans;
  add1 = randomNum(99);
  add2 = 10 * randomNum((98 - add1));
  add1 = 10 * add1;
  ans = add1 + add2;
  formula = [String(add1) + ' + ' + String(add2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function sub_xx0_and_xx0() {  //尾数是0三位数减法
  var formula, add1, add2, ans;
  add1 = randomNum(99);
  add2 = 10 * randomNum((98 - add1));
  add1 = 10 * add1;
  ans = add1 + add2;
  formula = [String(ans) + ' - ' + String(add2) + ' = [ ] ', add1];
  // console.log(formula);
  return formula;
}

function mul_add_in_x(x) {  //数字x以内乘加混合
  var formula, mul1, mul2, add1, ans;
  mul1 = randomNum(x);
  mul2 = randomNum(x);
  add1 = randomNum(x);
  ans = mul1 * mul2 + add1;
  formula = [String(mul1) + ' × ' + String(mul2) + ' + ' + String(add1) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function mul_sub_in_x(x) {  //数字x以内乘减混合
  var formula, mul1, mul2, sub1, ans;
  mul1 = randomNum(x);
  mul2 = randomNum(x);
  sub1 = randomNum(mul1 * mul2);
  ans = mul1 * mul2 - sub1;
  formula = [String(mul1) + ' × ' + String(mul2) + ' - ' + String(sub1) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function div_add_in_x(x) {  //数字x以内除加混合
  var formula, mul1, mul2, add1, ans;
  mul1 = randomNum(x);
  mul2 = randomNum(x);
  add1 = randomNum(x);
  ans = mul2 + add1;
  formula = [String((mul1 * mul2)) + ' ÷ ' + String(mul1) + ' + ' + String(add1) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function div_sub_in_x(x) {  //数字x以内除减混合
  var formula, mul1, mul2, sub1, ans;
  mul1 = randomNum(x);
  mul2 = randomNum(x);
  sub1 = randomNum(mul2);
  ans = mul2 - sub1;
  formula = [String((mul1 * mul2)) + ' ÷ ' + String(mul1) + ' - ' + String(sub1) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function mul_in_10_3() {  //表内三位连乘
  var formula, mul1, mul2, mul3, ans;
  mul1 = randomNum(3);
  mul2 = randomNum(3);
  mul3 = randomNum(9);
  ans = mul1 * mul2 * mul3;
  formula = [String(mul1) + ' × ' + String(mul2) + ' × ' + String(mul3) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function div_in_10_3() {  //10以内三位连除
  var formula, mul1, mul2, mul3, ans;
  mul1 = randomNum(3);
  mul2 = randomNum(3);
  mul3 = randomNum(9);
  ans = mul1 * mul2 * mul3;
  formula = [String(ans) + ' ÷ ' + String(mul3) + ' ÷ ' + String(mul2) + ' = [ ] ', mul1];
  // console.log(formula);
  return formula;
}

function div_mul_in_x_3(x) {  //10以内三位乘除混合
  var formula, mul1, mul2, mul3, ans;
  mul1 = randomNum(x);
  mul2 = randomNum(x);
  mul3 = randomNum(x);
  ans = mul2 * mul3;
  formula = [String((mul1 * mul2)) + ' ÷ ' + String(mul1) + ' × ' + String(mul3) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function add_mul_include_brackets(x) {  //数字x以内包括小括号的先加后乘三位运算
  var formula, add1, add2, mul1, ans;
  add1 = randomNum(x);
  add2 = randomNum((x - add1));
  mul1 = randomNum(x);
  ans = (add1 + add2) * mul1;
  formula = ['(' + String(add1) + ' + ' + String(add2) + ')' + ' × ' + String(mul1) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function sub_mul_include_brackets(x) {  //数字x以内包括小括号的先减后乘三位运算
  var formula, add1, add2, mul1, ans;
  add1 = randomNum(x);
  add2 = randomNum(add1, x);
  mul1 = randomNum(x);
  ans = (add2 - add1) * mul1;
  formula = ['(' + String(add2) + ' - ' + String(add1) + ')' + ' × ' + String(mul1) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function add_div_include_brackets(x) {  //数字x以内包括小括号的先加后除三位运算
  var formula, add1, add2, mul1, ans;
  ans = randomNum(x);
  mul1 = randomNum(x);
  add1 = randomNum(mul1 * ans);
  add2 = mul1 * ans - add1;
  formula = ['(' + String(add1) + ' + ' + String(add2) + ')' + ' ÷ ' + String(mul1) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function sub_div_include_brackets(x) {  //数字x以内包括小括号的先加后除三位运算
  var formula, add1, add2, mul1, ans;
  ans = randomNum(x);
  mul1 = randomNum(x);
  add1 = randomNum((mul1 * ans), 11 * x);
  add2 = add1 - (mul1 * ans);
  formula = ['(' + String(add1) + ' - ' + String(add2) + ')' + ' ÷ ' + String(mul1) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function mul_by_x_and_x0() {  //一位数乘以整十
  var formula, mul1, mul2, ans;
  mul1 = randomNum(9);
  mul2 = 10 * randomNum(9);
  ans = mul1 * mul2;
  formula = [String(mul1) + ' × ' + String(mul2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function div_have_remainder_in_x(x) {  //有余数除法
  var formula, mul1, mul2, remainder, ans;
  mul1 = randomNum(2, x);
  mul2 = randomNum(2, x);
  remainder = randomNum(1, mul2 - 1);
  ans = mul1 * mul2 + remainder;
  if (randomNum(0, 1) === 0) {  //要求填余数
    formula = [String(ans) + ' ÷ ' + String(mul2) + ' = ' + String(mul1) + ' ... [ ] ', remainder];
  } else {  //要求填商
    formula = [String(ans) + ' ÷ ' + String(mul2) + ' = [ ] ... ' + String(remainder), mul1];
  }
  // console.log(formula);
  return formula;
}

function mul_by_x0_and_x0() {  //整十乘以整十
  var formula, mul1, mul2, ans;
  mul1 = 10 * randomNum(9);
  mul2 = 10 * randomNum(9);
  ans = mul1 * mul2;
  formula = [String(mul1) + ' × ' + String(mul2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function mul_by_x_and_x00() {  //一位数乘以整百
  var formula, mul1, mul2, ans;
  mul1 = randomNum(9);
  mul2 = 100 * randomNum(9);
  ans = mul1 * mul2;
  formula = [String(mul1) + ' × ' + String(mul2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}

function mul_by_x00_and_x0() {  //整百乘以整十
  var formula, mul1, mul2, ans;
  mul1 = 100 * randomNum(9);
  mul2 = 10 * randomNum(9);
  ans = mul1 * mul2;
  formula = [String(mul1) + ' × ' + String(mul2) + ' = [ ] ', ans];
  // console.log(formula);
  return formula;
}


// add_in_x(7);
// sub_in_x(9);
// inverse_add_in_x(10);
// carry_add_in_x_(20);
// not_carry_add_in_x_(20);
// abd_sub_in_x(20);
// not_abd_sub_in_x(20);
// add_in_x_3(9);
// sub_in_x_3(9);
// add_sub_in_x(3);
// add_sub_in_x(9);
// mul_in_x(0, 6);
// mul_in_x(4, 9);
// div_in_xy(1, 6);
// div_in_xy(4, 9);
// not_carry_add_by_x000();
// sub_by_x000();
// carry_add_by_x0_x00_x000(1000);
// abd_sub_by_x0_x00_x000(1000);
// not_carry_add_xx0_and_xx0();
// sub_xx0_and_xx0();
// mul_add_in_x(9);
// mul_sub_in_x(9);
// div_add_in_x(9);
// div_sub_in_x(9);
// mul_in_10_3();
// div_in_10_3();
// div_mul_in_x_3(9);
// add_mul_include_brackets(9);
// sub_mul_include_brackets(9);
// add_div_include_brackets(9);
// sub_div_include_brackets(9);
// mul_by_x_and_x0();
// div_have_remainder_in_x(9);
// mul_by_x0_and_x0();
// mul_by_x00_and_x0();
